package game.views;

import motion.Actuate;
import game.shader.CosShader;
import game.worlds.World;
import hx.display.Scene;

/**
 * 角色加载准备界面
 */
@:build(hx.macro.UIBuilder.build("assets/views/LoadRoleView.xml"))
class LoadRoleView extends Scene {
	/**
	 * 至少加载页面要显示多少秒的时间单位
	 */
	public static var MIN_SHOW_TIME = #if game_develop 1 #else 5 #end;

	/**
	 * 创建角色信息
	 * @param id 
	 * @return LoadRoleViewPlayer
	 */
	public static function createLoadRoleViewPlayer(id:String):LoadRoleViewPlayer {
		return {
			id: id,
			name: id,
		};
	}

	/**
	 * 正在处于加载的世界
	 */
	public var world:World;

	public var player1s:Array<LoadRoleViewPlayer>;

	public var player2s:Array<LoadRoleViewPlayer>;

	public function isLoaded():Bool {
		return __isLoaded;
	}

	/**
	 * 绑定加载世界
	 * @param world 
	 */
	public function bindWorld(world:World, player1s:Array<LoadRoleViewPlayer>, player2s:Array<LoadRoleViewPlayer>):Void {
		this.world = world;
		this.player1s = player1s;
		this.player2s = player2s;
		this.l1.visible = false;
		this.l2.visible = false;
		this.l3.visible = false;
		this.r1.visible = false;
		this.r2.visible = false;
		this.r3.visible = false;
		for (index => item in player1s) {
			var itemDisplay:LoadRoleItem = cast this.getChildById("l" + (index + 1));
			itemDisplay.setData(item);
			itemDisplay.visible = true;
		}
		for (index => item in player2s) {
			var itemDisplay:LoadRoleItem = cast this.getChildById("r" + (index + 1));
			itemDisplay.setData(item, true);
			itemDisplay.visible = true;
		}
		this.updateEnabled = true;
		this.animte_bg.shader = CosShader.getInstance();
		this.quad.mouseEnabled = false;
		this.__minTime = MIN_SHOW_TIME;
	}

	/**
	 * 至少停留3秒
	 */
	private var __minTime = 5.;

	/**
	 * 是否已经加载完毕
	 */
	private var __isLoaded = false;

	override function onUpdate(dt:Float) {
		super.onUpdate(dt);
		CosShader.getInstance().update(dt);
		for (index => item in player1s) {
			var itemDisplay:LoadRoleItem = cast this.getChildById("l" + (index + 1));
			var bitmapData = this.world.assets.getBitmapData(item.id);
			if (bitmapData != null && !itemDisplay.head.visible) {
				itemDisplay.updateHead(bitmapData);
			}
		}
		for (index => item in player2s) {
			var itemDisplay:LoadRoleItem = cast this.getChildById("r" + (index + 1));
			var bitmapData = this.world.assets.getBitmapData(item.id);
			if (bitmapData != null && !itemDisplay.head.visible) {
				itemDisplay.updateHead(bitmapData);
			}
		}
		// 进度参考
		var p = this.world.assets.getProgress() * 0.5 + 0.5 * (Math.min(MIN_SHOW_TIME - __minTime, MIN_SHOW_TIME) / MIN_SHOW_TIME);
		this.progress.setProgress(p);
		__minTime -= dt;
		if (__minTime < 0 && p == 1) {
			__isLoaded = true;
			this.updateEnabled = false;
			Actuate.tween(quad, 0.5, {
				alpha: 1
			}).onComplete(() -> {
				this.world.onGameStart();
				this.releaseScene();
			});
		}
	}
}

/**
 * 展示的角色信息
 */
typedef LoadRoleViewPlayer = {
	/**
	 * 显示的角色名称
	 */
	var name:String;

	/**
	 * 角色ID
	 */
	var id:String;

	/**
	 * 显示的玩家昵称
	 */
	var ?playerName:String;
}
